#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:ouyang time:2023/4/3

from LIB.Listen import *
import networkx as nx
import matplotlib.pyplot as plt

CAR_NUM = 2
CAR_ID = "CAR"+str(CAR_NUM)
my_rsu = UDPListen()
my_rsu.start()
my_graph = nx.Graph()
my_graph.add_node(CAR_NUM)
subax = plt.subplot(121)


def listen_broadcast_task():
    while True:
        time.sleep(2)
        temp_list = my_rsu.get_broadcast_info()
        for m_temp in temp_list:
            number = int(m_temp["ID"][3:])
            my_graph.add_node(number)
            my_graph.add_edge(CAR_NUM, number, weight=m_temp["TIME_STAMP"])
        nx.draw(my_graph, with_labels=True, font_weight='bold')
        plt.show()


def draw_map_task():
    while True:
        time.sleep(1)


class ThreadListenBroadcast(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        listen_broadcast_task()


class ThreadDrawMap(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        draw_map_task()


deal_task = ThreadListenBroadcast()
deal_task.start()
